package com.best.nacos.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Jiang Akang
 * employeeId: BG435424
 * @date 2020/8/27
 **/

@RestController
@RefreshScope //加上此注解实时刷新配置
public class NacosConfigController {

    //通过value注解读取配置信息
    @Value("${common.name}")
    private String config1;

    //读取配置信息
    @GetMapping("/configs")
    public String getConfigs() {
        return config1;
    }

    //注入配置文件上下文
//    @Autowired
//    private ConfigurableApplicationContext applicationContext;

    //通过配置信息的方式来获取最新的配置
//    @GetMapping("/config-configuable")
//    public String getConfigsByConf() {
//        return applicationContext.getEnvironment().getProperty("common.name");
//    }

    //通过配置信息的方式来获取最新的配置
//    @GetMapping("/config1")
//    public String getConfigs1() {
//        String name = applicationContext.getEnvironment().getProperty("common.name");
//        String age = applicationContext.getEnvironment().getProperty("common.age");
//        String address = applicationContext.getEnvironment().getProperty("common.address");
//        return name + " " + age + " " + address;
//    }

}
